<?php

namespace Domotique\FeedBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Domotique\FeedBundle\Entity\Logs;

class ApiController extends Controller
{
    public function feedAction($name,$value)
    {

    	$doctrine = $this->getDoctrine()->getManager();

    	// On log
    	$log = new Logs();
    	$log->setLogName($name);
    	$log->setLogValue((int)$value);
    	$doctrine->persist($log);
    	
    	$doctrine->flush();

        return new Response(json_encode(array("response_code" => "ok")));
    }

    public function getAction($name)
    {
        $doctrine = $this->getDoctrine()->getManager();

        $stats = $doctrine->getRepository("FeedBundle:Logs")
            ->findBy(
                array(
                    "log_name" => $name
                )
            );
        foreach($stats as $log){
            $tabLogs[] = array(
                "name" => $log->getLogName(), 
                "value" => $log->getLogValue(), 
                "date" => date("d/m/Y H:i", $log->getDate()->getTimestamp())
            );
        }
        return new Response(json_encode(array("response_code" => "ok", "logs" => $tabLogs)));
    }
}
